#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   mouse_operation.py
@Time    :   2024/02/09 01:30:16
@Author  :   chakcy 
@Email   :   947105045@qq.com
@description   :   鼠标操作
'''

import pyautogui

class Mouse():
    # 初始化
    def __init__(self, pause:float=None, failsafe=None) -> None:
        if pause is not None:
            pyautogui.PAUSE = pause
        if failsafe is not None:
            pyautogui.FAILSAFE = failsafe

    # 延迟
    def sleep(self, n:float):
        pyautogui.sleep(n)

    # 获取屏幕分辨率
    def get_screen_size(self):
        return pyautogui.size()

    # 获取鼠标当前位置
    def get_mouse_position(self):
        return pyautogui.position()
    
    # 判断坐标是否在屏幕中
    def is_on_screen(self, x:int, y:int):
        return pyautogui.onScreen(x, y)
    
    # 移动鼠标
    def move_mouse(self, move_type, x:int, y:int, duration):
        def absolute_position(x, y, duration):
            pyautogui.moveTo(x, y, duration)

        def relative_position(x, y, duration):
            pyautogui.moveRel(x, y, duration)
            
        functions ={
            "绝对定位": absolute_position,
            "相对定位": relative_position
        }
        functions.get(move_type, lambda: "未知定位类型")(x, y, duration)
    
    # 鼠标点击
    def click_mouse(self, click_type):
        def left_click():
            pyautogui.leftClick()
        def right_click():
            pyautogui.rightClick()
        def middle_click():
            pyautogui.middleClick()
        def double_click():
            pyautogui.doubleClick()

        functions ={
            "左键": left_click,
            "右键": right_click,
            "中轮点击": middle_click,
            "左键双击": double_click
        }
        functions.get(click_type, lambda: "未知点击类型")()
    
    # 滑动滑轮
    def scroll(self, n:int):
        pyautogui.scroll(n)

    # 拖动
    def drag(self, drag_type, x:int, y:int, duration:float):
        def absolute_position(x, y, duration):
            pyautogui.dragTo(x, y, duration=duration)

        def relative_position(x, y, duration):
            pyautogui.drag(x, y, duration=duration)

        functions ={
            "绝对定位": absolute_position,
            "相对定位": relative_position
        }
        functions.get(drag_type, lambda: "未知定位类型")(x, y, duration)

    # 图片点击
    def image_click(self, image_path:str, click_type:str, threshold:float=0.8, duration=1, offset=(0,0)):
        center_x, center_y = pyautogui.locateCenterOnScreen(image_path, confidence=threshold)
        self.move_mouse("绝对定位" , center_x/2 - offset[0], center_y/2 - offset[1], duration)
        self.click_mouse(click_type)
    